package cn.chencq.moudle.lottery.controller;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.chencq.exception.MyException;
import cn.chencq.moudle.lottery.entity.LotteryAnalysis;
import cn.chencq.moudle.lottery.service.LotteryAnalysisService;
import cn.chencq.moudle.lottery.service.LotteryService;
import cn.chencq.page.PageBean;
import cn.chencq.page.PageParam;
import cn.chencq.utils.DateUtils;
import cn.chencq.utils.R;

@RestController
@RequestMapping("/lottery")
public class LotteryController {

	private static final Logger logger = LoggerFactory.getLogger(LotteryController.class);

	@Autowired
	private LotteryService lotteryService;

	@Autowired
	private LotteryAnalysisService lotteryAnalysisService;

	/**
	 * 
	 * @Title: queryLottery
	 * @Description: 按条件分页查询接口
	 * @param pageParam
	 * @return R
	 */
	@RequestMapping("/queryLottery")
	public R queryLottery(@ModelAttribute PageParam pageParam) {
		PageBean page = lotteryService.listPage(pageParam);
		return R.ok().put("page", page);
	}

	/**
	 * 
	 * @Title: syncLotteryData
	 * @Description: 从第三方同步数据到本地数据库
	 * @param params
	 * @return R
	 */
	@RequestMapping("/syncLottery")
	public R syncLotteryData(@RequestBody Map<String, String> params) {
		String startTime = params.get("startTime");
		String endTime = params.get("endTime");
		String lotteryType = params.get("lotteryType");
		long count = lotteryService.syncData(startTime, endTime, lotteryType);
		return R.ok().put("count", count);
	}

	/**
	 * 
	 * @Title: syncLotteryCount
	 * @Description: 重新统计分析
	 * @param params
	 * @return R
	 */
	@RequestMapping("/syncLotteryCount")
	public R syncLotteryCount(@RequestBody Map<String, String> params) {
		String startTime = params.get("startTime");
		String endTime = params.get("endTime");
		String lotteryType = params.get("lotteryType");

		Date startDate = null;
		Date endDate = null;
		if (!StringUtils.isEmpty(startTime)) {
			startDate = DateUtils.str2Date(startTime, null);
		}

		if (!StringUtils.isEmpty(endTime)) {
			endDate = DateUtils.str2Date(endTime, null);
		}

		int count = 0;
		if (Objects.nonNull(startDate) && Objects.nonNull(endDate)) {
			while (startDate.compareTo(endDate) <= 0) {
				String dateStr = DateUtils.date2Str(startDate, null);
				LotteryAnalysis la = lotteryAnalysisService.groupCount(dateStr, lotteryType);
				lotteryAnalysisService.saveOrUpdate(la);
				count++;
				logger.info("统计分析，lotteryType:{}, lotteryDate:{}", lotteryType, dateStr);
				startDate = DateUtils.addDay(startDate, 1);
			}
		} else {
			String dateStr = startTime;
			if (Objects.isNull(dateStr)) {
				dateStr = DateUtils.date2Str(new Date(), null);
			}
			LotteryAnalysis la = lotteryAnalysisService.groupCount(dateStr, lotteryType);
			lotteryAnalysisService.saveOrUpdate(la);
			count++;
			logger.info("统计分析，lotteryType:{}, lotteryDate:{}", lotteryType, dateStr);
		}

		return R.ok().put("count", count);
	}

	@RequestMapping("/predict")
	public R bpPredict(String lotteryType, String lotteryId) {
		if (StringUtils.isBlank(lotteryType)) {
			throw new MyException("彩票类型不能为空");
		}

		JSONObject res = lotteryService.predict(lotteryType, lotteryId);
		return R.ok().put("predict", res);
	}

	@RequestMapping("/follow")
	public R followAlgorithm(String lotteryType, String lotteryDate) {
		if (StringUtils.isBlank(lotteryType)) {
			throw new MyException("彩票类型不能为空");
		}

		if (StringUtils.isBlank(lotteryDate)) {
			lotteryDate = DateUtils.date2Str(new Date(), null);
		}

		double income = lotteryAnalysisService.followAlgorithm(lotteryDate, lotteryType);

		return R.ok().put("income", income);
	}
}
